//
//  CAAddUserInfoViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAAddUserInfoViewController.h"
#import "CAUserInfoView.h"
#import "CAUpdateUserInfoApi.h"
#import "CAUpgradeViewController.h"

@interface CAAddUserInfoViewController ()

@property (nonatomic, strong) CAUserInfoView *userInfoView;
@property (nonatomic, strong) UIButton *saveButton;

@end

@implementation CAAddUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.userInfoView];
    [self.view addSubview:self.saveButton];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"代理商申请资料";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.saveButton.mas_top);
    }];
    
    [_saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (isIPhoneX) {
            make.bottom.equalTo(self.view).offset(-34);
        } else {
            make.bottom.equalTo(self.view);
        }
        make.height.mas_equalTo(44);
    }];
}

#pragma mark get & set
- (CAUserInfoView *)userInfoView {
    if (!_userInfoView) {
        _userInfoView = [CAUserInfoView new];
    }
    return _userInfoView;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [UIButton new];
        [_saveButton setTitle:@"下一步" forState:UIControlStateNormal];
        _saveButton.backgroundColor = RGBCOLOR(233, 85, 19);
        _saveButton.titleLabel.font = [UIFont systemFontOfSize:20];
        
        [[_saveButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self saveInfoAction];
        }];
    }
    return _saveButton;
}

#pragma mark private method
- (void) saveInfoAction {
    NSString *wechat = _userInfoView.wechatTextField.text;
    NSString *cId = _userInfoView.cidTextField.text;
    NSString *name = _userInfoView.nameTextField.text;
    CAUpdateUserInfoApi *updateUserInfoApi = [[CAUpdateUserInfoApi alloc] initWithWechat:wechat CId:cId CIdName:name];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [updateUserInfoApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAUpdateUserInfoApi *api = (CAUpdateUserInfoApi *) request;
        if (api.done) {
            GVUD.cid = cId;
            GVUD.wechat = wechat;
            GVUD.cidName = name;
            CAUpgradeViewController *upgradeVC = [CAUpgradeViewController new];
            [self.navigationController pushViewController:upgradeVC animated:YES];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

@end
